//package medicalApp.velociraptors.com;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/**
 * Creates the Dialog for Adding an Appointment
 * 
 * @author Vishnu
 * @author Danny Cook
 */
@SuppressWarnings("serial")
public class AddAppointmentDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	
	private Connection con;
	
	private final JTextField txtReason;
	
	private final JTextField txtDoctorFirstName;
	
	private final JTextField txtDoctorLastName;
	
	private final JTextField txtYear;
	
	private final JTextField txtMonth;
	
	private final JTextField txtDays;
	
	private final JTextField txtHours;
	
	private final JTextField txtMinutes;
	
	protected Integer drSSN;


	/**
	 * Create the dialog.
	 * 
	 * @param mac The MedAppController that powers this dialog
	 * @param SSN The SSN of the patient who is accessing the class.
	 */
	public AddAppointmentDialog(final MedAppController mac, final int SSN) {
		con = mac.getCon();
		setBounds(100, 100, 686, 427);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		final JLabel lblDateAndTime = new JLabel("Date and Time: ");
		lblDateAndTime.setBounds(37, 48, 114, 14);
		contentPanel.add(lblDateAndTime);
		
		final JLabel lblReason = new JLabel("Reason: ");
		lblReason.setBounds(37, 141, 75, 14);
		contentPanel.add(lblReason);
		
		final JLabel lblDoctorFirstName = new JLabel("First name of Doctor: ");
		lblDoctorFirstName.setBounds(37, 234, 232, 14);
		contentPanel.add(lblDoctorFirstName);
		
		final JLabel lblDoctorLastName = new JLabel("Last name of Doctor: ");
		lblDoctorLastName.setBounds(37, 287, 183, 14);
		contentPanel.add(lblDoctorLastName);
		
		final JLabel lblDoctorSSN = new JLabel("Doctor ID #: ");
		lblDoctorSSN.setBounds(37, 340, 232, 14);
		contentPanel.add(lblDoctorSSN);
		
		txtReason = new JTextField();
		txtReason.setBounds(306, 137, 245, 22);
		contentPanel.add(txtReason);
		txtReason.setColumns(3000);
		
		txtDoctorFirstName = new JTextField();
		txtDoctorFirstName.setBounds(306, 226, 245, 22);
		contentPanel.add(txtDoctorFirstName);
		txtDoctorFirstName.setColumns(15);
		
		txtDoctorLastName = new JTextField();
		txtDoctorLastName.setBounds(306, 287, 245, 22);
		contentPanel.add(txtDoctorLastName);
		txtDoctorLastName.setColumns(15);
		
		txtYear = new JTextField();
		txtYear.setBounds(306, 41, 65, 28);
		contentPanel.add(txtYear);
		txtYear.setColumns(10);
		
		txtMonth = new JTextField();
		txtMonth.setColumns(10);
		txtMonth.setBounds(394, 41, 65, 28);
		contentPanel.add(txtMonth);
		
		txtDays = new JTextField();
		txtDays.setColumns(10);
		txtDays.setBounds(486, 41, 65, 28);
		contentPanel.add(txtDays);
		
		final JLabel lblYyyy = new JLabel("YYYY");
		lblYyyy.setBounds(273, 47, 34, 16);
		contentPanel.add(lblYyyy);
		
		final JLabel lblMm = new JLabel("MM");
		lblMm.setBounds(372, 48, 27, 16);
		contentPanel.add(lblMm);
		
		final JLabel lblDd = new JLabel("DD");
		lblDd.setBounds(470, 47, 18, 16);
		contentPanel.add(lblDd);
		
		final JLabel lblHh = new JLabel("hh");
		lblHh.setBounds(273, 81, 14, 16);
		contentPanel.add(lblHh);
		
		txtHours = new JTextField();
		txtHours.setBounds(306, 75, 65, 28);
		contentPanel.add(txtHours);
		txtHours.setColumns(10);
		
		JLabel lblMm_1 = new JLabel("mm");
		lblMm_1.setBounds(372, 81, 24, 16);
		contentPanel.add(lblMm_1);
		
		txtMinutes = new JTextField();
		txtMinutes.setBounds(404, 75, 54, 28);
		contentPanel.add(txtMinutes);
		txtMinutes.setColumns(10);		
		
		final String sql = "SELECT `D_SSN` FROM `doctor";
		final ArrayList<Integer> doctorSSNs = new ArrayList<Integer>();
		try{
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				doctorSSNs.add(rs.getInt(1));
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		Object[] array = doctorSSNs.toArray();
		
		JComboBox cmbDoctorSSNs = new JComboBox(array);
		cmbDoctorSSNs.setSelectedIndex(0);
		drSSN = (Integer) cmbDoctorSSNs.getSelectedItem();
		cmbDoctorSSNs.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				JComboBox target = (JComboBox)arg0.getSource();
				drSSN = (Integer) target.getSelectedItem();

			}
		});
		cmbDoctorSSNs.setBounds(306, 334, 245, 20);
		contentPanel.add(cmbDoctorSSNs);

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					/**
					 * Designed to perform the action specified upon triggering.
					 * 
					 * @param arg0 The ActionEvent that triggers the method call.
					 */ 
					public void actionPerformed(ActionEvent arg0) {
						String dateAndTime = txtYear.getText() + "-" + txtMonth.getText() + "-" + txtDays.getText() + " " + txtHours.getText() + ":" + txtMinutes.getText() + ":" + "00";
						mac.out.println("Appointment date and time: " + dateAndTime);
						String reason = txtReason.getText();
//						int ssn = Integer.parseInt(txtSSN.getText());
						String drFirstName = txtDoctorFirstName.getText();
						String drLastName=txtDoctorLastName.getText();
//						int drSSN = Integer.parseInt(txtDoctorSSN.getText());
						
						String sql = "INSERT INTO `velociraptorsattack`.`appointment` (`DATEANDTIME`,`REASON`,`A_FORPATIENTSSN`,`A_DoctorFirstName`,`A_DoctorLastName`,`A_BYDOCTORSSN`) VALUES('" + Timestamp.valueOf(dateAndTime) + "','" + reason + "','" + SSN + "','" + drFirstName + "','" + drLastName + "','" + drSSN.intValue() + "')";
						try {
							Statement stmt = con.createStatement();
							int rs = stmt.executeUpdate(sql);
							JOptionPane.showMessageDialog(null, "Appointment Created!");
							dispose();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							JOptionPane.showMessageDialog(null, "Appointment Fail!","FAIL",JOptionPane.ERROR_MESSAGE);	
							e.printStackTrace();
						}
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					/**
					 * Designed to perform the action specified upon triggering.
					 * 
					 * @param arg0 The ActionEvent that triggers the method call.
					 */ 
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
